package com.study.springbootsecurity.filter;

import com.study.springbootsecurity.entity.LoginUser;
import com.study.springbootsecurity.service.impl.UserDetailsServiceImpl;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @Description: TODO 使用认证提供者报错 自定义userdetails和默认userDetails无法指定，认证管理器栈溢出
 * @author: Huangjianyun
 * @date: 2025-06-07 17:10
 */
//public class LoginAuthenticationProvider implements AuthenticationProvider {
//
//    private PasswordEncoder passwordEncoder;
//
//    private UserDetailsServiceImpl userDetailsServiceImpl;
//
//    public LoginAuthenticationProvider(PasswordEncoder passwordEncoder, UserDetailsServiceImpl userDetailsServiceImpl) {
//        this.passwordEncoder = passwordEncoder;
//        this.userDetailsServiceImpl = userDetailsServiceImpl;
//
//    }
//
//    @Override
//    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
//        String username = authentication.getName();
//        String password = authentication.getCredentials().toString();
//        LoginUser user = (LoginUser)userDetailsServiceImpl.loadUserByUsername(username);
//        boolean matches  = passwordEncoder.matches(password,user.getSysUser().getPassword());
//
//        if(!matches){
//            // 使用标准异常
//            throw new BadCredentialsException("用户或密码不正确");
//        }
//        return new UsernamePasswordAuthenticationToken(user,null,user.getAuthorities());
//    }
//
//    @Override
//    public boolean supports(Class<?> authentication) {
//        return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
//    }
//}
